package lanou3g.myfirstapp36kr.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;
import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.entity.FragmentNewsLVEntity;
import lanou3g.myfirstapp36kr.entity.FragmentNewsLVEntity.DataBean.DataBean1;

/**
 * Created by dllo on 16/8/3.
 */
public class FragmentNewsLVAdapter extends BaseAdapter {
//    private FragmentNewsLVEntity entity;
    private Context context;
    Date curData = new Date(System.currentTimeMillis());
    SimpleDateFormat format = new SimpleDateFormat("yyyy:MM:dd: HH:mm:ss");
    private List<DataBean1> entities;


    public FragmentNewsLVAdapter(Context context) {
        this.context = context;
        entities = new ArrayList<>();
    }

    public void setEntity(FragmentNewsLVEntity entity) {
        this.entities.clear();
        this.entities.addAll(entity.getData().getData());
        //Log.d("FragmentNewsLVAdapter", "entities:" + entities);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return entities == null ? 0 : entities.size();
    }

    @Override
    public Object getItem(int i) {
        return entities.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_fragment_news_lv,viewGroup,false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Picasso.with(context).load(entities.get(i).getFeatureImg()).placeholder(R.mipmap.ic_launcher).into(viewHolder.imageView);
        viewHolder.title.setText(entities.get(i).getTitle());
        viewHolder.author.setText(entities.get(i).getUser().getName());
        viewHolder.date.setText(format.format(curData));
        viewHolder.type.setText(entities.get(i).getColumnName());
        return view;
    }
    private class ViewHolder{
        private ImageView imageView;
        private TextView title;
        private TextView author;
        private TextView date;
        private TextView type;

        public ViewHolder(View view) {
            imageView = (ImageView) view.findViewById(R.id.iv_item_f_news_lv_picture);
            title = (TextView) view.findViewById(R.id.tv_item_f_news_lv_title);
            author = (TextView) view.findViewById(R.id.tv_item_f_news_lv_author);
            date = (TextView) view.findViewById(R.id.tv_item_f_news_lv_data);
            type = (TextView) view.findViewById(R.id.tv_item_f_news_lv_type);
        }
    }
}
